//
//  DeviceItemCell.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/6/6.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "DeviceItemCell.h"

@implementation DeviceItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(DeviceItem *)model {

    [[RACObserve(model, sn) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        if (model.label) {
            self.deviceLb.text = [NSString stringWithFormat:@"设备：%@", model.label];
        } else {
            self.deviceLb.text = [NSString stringWithFormat:@"设备：%@", model.sn];
        }
        self.snLb.text = [NSString stringWithFormat:@"s/n:%@", model.sn];
    }];
    
    [[RACObserve(model, deviceState) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        DeviceState *state = model.deviceState;
        CGSize size = self.statusBt.frame.size;
        if (model.deviceOnlineState && [model.deviceOnlineState intValue] == 0) {
            [self.statusBt setTitle:@"   设备离线  " forState:UIControlStateNormal];
            [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#FF0000")) forState:UIControlStateNormal];
        } else if (state) {
            if (!state.disks || (state.disks && state.disks.count<=0)) {
                [self.statusBt setTitle:@"  磁盘未接入  " forState:UIControlStateNormal];
                [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#FF0000")) forState:UIControlStateNormal];
            } else if ([state.miningState intValue]== 1) {
                [self.statusBt setTitle:@"   正在挖矿  " forState:UIControlStateNormal];
                [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#15c1ff")) forState:UIControlStateNormal];
            } else if ([state.miningSwitch intValue] == 0) {
                [self.statusBt setTitle:@"   挖矿未开启  " forState:UIControlStateNormal];
                [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#2dff9e")) forState:UIControlStateNormal];
            } else {
                [self.statusBt setTitle:@"   挖矿故障  " forState:UIControlStateNormal];
                [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#FF0000")) forState:UIControlStateNormal];
            }
        } else {
            [self.statusBt setTitle:@"   未知错误  " forState:UIControlStateNormal];
            [self.statusBt setBackgroundImage:createImageOfLC(size.height/2, size, colorWithString(@"#FF0000")) forState:UIControlStateNormal];
        }
    }];
}



@end
